<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_pembelian_produk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_pembelian_produk_model', 'IspPembelianP');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Penjualan Produk";	
		
		viewByTemplate('isp_pembelian_produk/index', $data, 'grid');
	}
	
	function id_auto(){
		$query = $this->db->query("SELECT id_pembelian FROM (SELECT * FROM isp_pembelian_produk ORDER BY id_pembelian DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_pembelian'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_pembelian_auto'] = $autonum;
		echo json_encode($data);
	}

	function getdata()
	{
		$this->secure_session();
		echo $this->IspPembelianP->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspPembelianP->GetJSON2();
	}

	function getdata3()
	{
		$this->secure_session();
		echo $this->IspPembelianP->GetJSON3();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_customer") == '') {
			$error->Add("kode_customer", "Kode customer", $tpl_empty);
		
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
			    
				$data	=  array(
						'kode_customer' => $this->input->post('kode_customer'),
						//'nama_customer' => $this->input->post('nama_customer'),
						//'nama_perusahaan' => $this->input->post('nama_perusahaan'),
						'kode_produk' => $this->input->post('kode_inventori'),
						'nama_produk' => $this->input->post('nama_inventori'),
						'stock' => $this->input->post('banyak'),
						'harga_satuan' => $this->input->post('harga'),
						'jumlah' => $this->input->post('jumlah'),
						'total_bayar' => $this->input->post('total_bayar'),
						//'banyak' => $this->input->post('update_stock'),
						'id_pembelian' => $this->input->post('id_pembelian')
					);


				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspPembelianP->Add($data);

					$cek_data = $this->db->query("UPDATE isp_inventori SET banyak='".$this->input->post('update_stock')."' WHERE kode_inventori ='".$this->input->post('kode_inventori')."' ");

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					
					$cek_data = $this->db->query("UPDATE isp_inventori SET banyak='".$this->input->post('update_stock')."' WHERE kode_inventori ='".$this->input->post('kode_inventori')."' ");

					$this->IspPembelianP->Update($data, array('id_pembelian'	=> $data['id_pembelian']));
					//$this->IspPembelianP->UpdateStock($data, array('banyak'	=> $this->input->post('update_stock')));

				$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan = "Data Terhapus";
		$no_id 	= get('id_pembelian');
		
		if ($no_id != "")
		{
			// Delete Single
			$arr_rule	= array('id_pembelian' => $id_pembelian);
			$ls_result = $this->IspPembelianP->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_pembelian' => $row['id_pembelian']);
				$ls_result .= $this->IspPembelianP->DeleteSingle($arr_rule);
				//$ls_response = "Data Terhapus";
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>